/**
 * @FileName: BaseQuestionElement.java
 * @Package: top.swimmer.demo.question
 * @author youshipeng
 * @created 2017/5/2 14:29
 * <p>
 * Copyright 2016 ziroom
 */
package top.swimmer.demo.question;

import java.util.List;

import static top.swimmer.demo.nlp.SegmentUtils.getMaintainThing;
import static top.swimmer.demo.nlp.SegmentUtils.getMobile;
import static top.swimmer.demo.nlp.SegmentUtils.getPersonName;
import static top.swimmer.tools.str.StringUtils.isEmpty;

/**
 * <p></p>
 *
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author youshipeng
 * @since 1.0
 * @version 1.0
 */
public abstract class BaseQuestionElement {
    private boolean decision = false; // 焦点信息完整后，是否开始动作
    private boolean begin = false; // 是否开始获取焦点信息
    private String backAsk; // 反问
    private QuestionElement currentQuestionElement;

    public abstract List<QuestionElement> getQuestionElements();

    public abstract String beginAsk();

    public String backAsk() {
        if (!decision) {
            return beginAsk();
        }

        for (QuestionElement questionElement : getQuestionElements()) {
            if (isEmpty(questionElement.getValue())) {
                currentQuestionElement = questionElement;
                return questionElement.getFocus().getBackAsk();
            }
        }
        return null;
    }

    public boolean isElementsFull() {
        for (QuestionElement questionElement : getQuestionElements()) {
            if (isEmpty(questionElement.getValue())) {
                return false;
            }
        }
        return true;
    }

    public void extractFocus(String input) {
        if (!decision) {
            decision = true;
            return;
        }
        extractFocus(currentQuestionElement, input);
    }

    private void extractFocus(QuestionElement questionElement, String input) {
        switch (questionElement.getFocus()) {
            case MOBILE:
                questionElement.setValue(getMobile(input));
                break;
            case ADDRESS:
                questionElement.setValue(input);
                break;
            case NAME:
                questionElement.setValue(getPersonName(input));
                break;
            case MAINTAIN_THING:
                questionElement.setValue(getMaintainThing(input));
                break;
        }
    }

    public String getBackAsk() {
        return backAsk;
    }

    public void setBackAsk(String backAsk) {
        this.backAsk = backAsk;
    }

    public boolean isBegin() {
        return begin;
    }

    public void setBegin(boolean begin) {
        this.begin = begin;
    }

    public boolean isDecision() {
        return decision;
    }

    public void setDecision(boolean decision) {
        this.decision = decision;
    }
}